package com.poss.gui;

import com.poss.gui.api.PossGuiAction;
import com.poss.gui.api.PossMainAction;
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.Image;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JPanel;
import javax.swing.UIManager;

/**
 *
 * @author shan
 */
class PossMainForm extends JFrame implements PossGuiWindow {
    
    private JPanel centerPanel;
    private JMenuBar menubar;
    private JMenu menuApp;
    private JMenuItem menuItemExit;
    private JMenuItem menuItemUser;
    private PossMainAction mainActionBase;
    private boolean actionOk;

    public PossMainForm() {
        actionOk = false;
        initUi();
    }
    
    private void initUi() {
        try {
            UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
        } catch (Exception ex) {
            System.out.println(ex);
        }
        
        initMenuBar();
        initActions();
        
        setDefaultLookAndFeelDecorated(true);
        setLayout(new BorderLayout());
        centerPanel = new JPanel();
        add(centerPanel, BorderLayout.CENTER);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setIconImage(getApplicationIcon("../../images/application.png"));
        setVisible(true);
        setTitle("POSS");
        setMinimumSize(new Dimension(500, 400));
        setExtendedState(getExtendedState() | JFrame.MAXIMIZED_BOTH);       
    }
    
    private Image getApplicationIcon(String imagePath) {
        java.net.URL imgURL = PossMainForm.class.getResource(imagePath);
        if (imgURL != null) {
            return new ImageIcon(imgURL).getImage();
        } else {
            return null;
        }
    }
    
    private void initMenuBar() {
        menubar = new JMenuBar();

        /* Creating Menus */
        menuApp = new JMenu("System");
        
        /* Creating Menu Items */
        menuItemUser = new JMenuItem("User", new ImageIcon(PossMainForm.class.getResource("../../images/user.png")));
        menuItemExit = new JMenuItem("Exit", new ImageIcon(PossMainForm.class.getResource("../../images/exit.png")));
        
        menuApp.add(menuItemUser);
        menuApp.add(menuItemExit);
        menubar.add(menuApp);
        setJMenuBar(menubar);
    }
    
    @Override
    public boolean registerAction(PossGuiAction actionObject) {
        if (actionObject instanceof PossMainAction) {
            mainActionBase = (PossMainAction)actionObject;
            actionOk = true;
            return true;
        }
        actionOk = false;
        return false;
    }

    @Override
    public void showGui() {
        setVisible(true);
    }

    @Override
    public void hideGui() {
        setVisible(false);
    }

    private void initActions() {
        menuItemUser.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                if (actionOk) {
                    mainActionBase.userMenuItemClicked();
                } else {
                    System.out.println("Action registration has been failed"); // should be written in gui_log.log
                }
            }
        });
        
        menuItemExit.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                if (actionOk) {
                    mainActionBase.exitMenuItemClicked();
                } else {
                    System.out.println("Action registration has been failed"); // should be written in gui_log.log
                }
            }
        });
    }
    
}
